package com.asset.dispoal.file.bank.domain.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.asset.dispoal.file.bank.api.FileBankService;
import com.asset.dispoal.file.bank.domain.dto.FileBankDetailExportTemplate;
import com.asset.dispoal.file.bank.domain.dto.FileBankExportTemplate;
import com.asset.dispoal.file.bank.domain.service.command.FileBankDeleteCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankDetailDeleteCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankDetailExcelDataSaveCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankDetailModifyCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankDetailPageCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankDetailSaveCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankDetailStaitsticsCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankExcelDataSaveCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankExportCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankGetByIdCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankModifyCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankPageCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankQueryAllCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankQueryLevelAmountCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankQueryLevelCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankQueryStatisticsCommand;
import com.asset.dispoal.file.bank.domain.service.command.FileBankSaveCommand;
import com.asset.dispoal.file.bank.domain.service.excelListener.FileBankDetailExcelListener;
import com.asset.dispoal.file.bank.domain.service.excelListener.FileBankExcelListener;
import com.asset.dispoal.file.bank.dto.FileBankDetailPageRequest;
import com.asset.dispoal.file.bank.dto.FileBankDetailRequest;
import com.asset.dispoal.file.bank.dto.FileBankExportRequest;
import com.asset.dispoal.file.bank.dto.FileBankPageRequest;
import com.asset.dispoal.file.bank.dto.FileBankRequest;
import com.asset.dispoal.file.bank.dto.FileBankStatisticsRequest;
import com.asset.dispoal.file.bank.dto.FileBankStatisticsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import tech.mhuang.pacebox.springboot.protocol.Result;

import javax.servlet.http.HttpServletResponse;

@Service
public class FileBankServiceimpl implements FileBankService {

    @Autowired
    private FileBankDeleteCommand deleteCommand;

    @Autowired
    private FileBankSaveCommand saveCommand;

    @Autowired
    private FileBankModifyCommand modifyCommand;

    @Autowired
    private FileBankPageCommand pageCommand;


    @Autowired
    private FileBankDetailDeleteCommand deleteDetailCommand;

    @Autowired
    private FileBankDetailSaveCommand saveDetailCommand;

    @Autowired
    private FileBankDetailModifyCommand modifyDetailCommand;

    @Autowired
    private FileBankDetailPageCommand pageDetailCommand;

    @Autowired
    private FileBankQueryLevelCommand queryLevelCommand;

    @Autowired
    private FileBankQueryLevelAmountCommand queryLevelAmountCommand;

    @Autowired
    private FileBankDetailStaitsticsCommand staitsticsCommand;

    @Autowired
    private FileBankExportCommand exportCommand;

    @Override
    public Result page(FileBankPageRequest request) {
        return pageCommand.execute(request);
    }

    @Override
    public Result save(FileBankRequest request) {
        return saveCommand.execute(request);
    }

    @Override
    public Result modify(FileBankRequest request) {
        return modifyCommand.execute(request);
    }

    @Override
    public Result delete(String id) {
        return deleteCommand.execute(id);
    }

    @Autowired
    private Environment environment;

    @Autowired
    private FileBankExcelDataSaveCommand dataSaveCommand;

    @Override
    public void uploadExcel(String filepath) {
        String filePath = environment.getProperty("file.evidence.url") + "/" + filepath;
        ExcelReaderBuilder read = EasyExcel.read(filePath, FileBankExportTemplate.class, new FileBankExcelListener(dataSaveCommand));
        read.sheet().doRead();
    }

    @Override
    public Result saveDetail(FileBankDetailRequest request) {
        return saveDetailCommand.execute(request);
    }

    @Override
    public Result modifyDetail(FileBankDetailRequest request) {
        return modifyDetailCommand.execute(request);
    }

    @Override
    public Result pageDetail(FileBankDetailPageRequest request) {
        return pageDetailCommand.execute(request);
    }

    @Override
    public Result deleteDetail(String id) {
        return deleteDetailCommand.execute(id);
    }

    @Autowired
    private FileBankDetailExcelDataSaveCommand dataDetailSaveCommand;

    @Override
    public void uploadDetailExcel(String filepath, String bankId) {
        String filePath = environment.getProperty("file.evidence.url") + "/" + filepath;
        ExcelReaderBuilder read = EasyExcel.read(filePath, FileBankDetailExportTemplate.class,
                new FileBankDetailExcelListener(dataDetailSaveCommand, bankId));
        read.sheet().doRead();
    }

    @Override
    public Result queryByLevel() {
        return queryLevelCommand.execute();
    }

    @Override
    public Result queryByLevelAmount() {
        return queryLevelAmountCommand.execute();
    }

    @Override
    public Result queryDetailStatistics(String bankId) {
        return staitsticsCommand.execute(bankId);
    }

    @Override
    public void export(HttpServletResponse response, FileBankExportRequest request) {
        exportCommand.execute(response, request);
    }

    @Autowired
    private FileBankQueryAllCommand queryAllCommand;


    @Override
    public Result queryAll() {
        return queryAllCommand.execute();
    }

    @Autowired
    private FileBankGetByIdCommand bankGetByIdCommand;

    @Override
    public Result getById(String id) {
        return bankGetByIdCommand.execute(id);
    }

    @Autowired
    FileBankQueryStatisticsCommand queryStatisticsCommand;

    @Override
    public Result<FileBankStatisticsResponse> queryStatistics(FileBankStatisticsRequest statisticsRequest) {
        return queryStatisticsCommand.execute(statisticsRequest);
    }
}
